/**
 * 代理商成本费率列表
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="margin-t10">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>代理商成本费率列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="boxBlock"> 
        <div class="serachTop"> 
              <!-- 搜索筛选 -->
            <el-form :inline="true" :model="formInline" class="user-search">
              <!-- <el-form-item >
                <el-input
                  size="small"
                  v-model="formInline.name"
                  placeholder="组名"
                  clearable
                ></el-input>
              </el-form-item> -->
              <el-form-item>
                <el-input
                  size="small"
                  v-model="formInline.userId"
                  placeholder="输入用户ID"
                  type="number"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-search"
                  @click="search"
                >
                  搜索
                </el-button> 
              </el-form-item>
            </el-form>
        </div>
  
  
    <!--列表-->
    <div class="ra_box">
      <el-table
          size="small"
          :data="listData"
          highlight-current-row
          v-loading="loading"
          element-loading-text="拼命加载中"
          style="width: 100%"
        >
          <el-table-column prop="userId" label="用户ID" min-width="80" fixed="left" >
          </el-table-column>
          <el-table-column prop="params.iamCount" label="IAM" min-width="110"> </el-table-column> 
          <el-table-column
            prop="loginName"
            label="账户名称"
            show-overflow-tooltip
            min-width="200"
          >
          </el-table-column>
          <el-table-column prop="userName" label="昵称" show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="balance"
            label="账户余额"
            min-width="150"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div>{{ b(scope.row.balance) }}</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="cardBalance"
            label="卡余额"
            min-width="150"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div>
                {{
                  b(
                    scope.row.params.cardBalance
                      ? scope.row.params.cardBalance
                      : 0
                  )
                }}
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="count" label="卡Bin" show-overflow-tooltip>
            <template slot-scope="scope">
              <div>{{ scope.row.params.count }}</div>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="refuseCount"
            label="拒付率"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div>
                {{
                  scope.row.params.transactionCount == 0
                    ? "0%"
                    : (
                        (scope.row.params.refuseCount /
                          scope.row.params.transactionCount) *
                        100
                      ).toFixed(0) + "%"
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="refundCount"
            label="退款率"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div>
                {{
                  scope.row.params.transactionCount == 0
                    ? "0%"
                    : (
                        (scope.row.params.refundCount /
                          scope.row.params.transactionCount) *
                        100
                      ).toFixed(0) + "%"
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="refundAmount"
            label="退款金额率"
            show-overflow-tooltip
            min-width="160"
          >
            <template slot-scope="scope">
              <div>
                {{
                  scope.row.params.transactionAmount == 0
                    ? "0%"
                    : (
                        (scope.row.params.refundAmount /
                          scope.row.params.transactionAmount) *
                        100
                      ).toFixed(0) + "%"
                }}
              </div>
            </template>
          </el-table-column>  -->

          <!-- <el-table-column
            prop="createBy"
            label="操作人"
            show-overflow-tooltip
            min-width="120"
          >
        </el-table-column> 
           
          <el-table-column
            prop="createTime"
            label="创建时间"
            min-width="180"
            show-overflow-tooltip
          >
          </el-table-column> -->
      
          <el-table-column align="center" fixed="right" label="操作" min-width="180">
            <template slot-scope="scope"> 
              <el-button
                size="mini" 
                type="primary"
                @click="handleEdit(scope.$index, scope.row)" 
                style="color: #fff; cursor: pointer;"  
              >
                配置
              </el-button> 
              
            </template>
          </el-table-column>
        </el-table>
    </div>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
  </div>
    <!-- 新增界面  -->
    <el-dialog
      :close-on-click-modal="false"
      title="配置"
      :visible.sync="addFormVisible"
      min-width="900px"
      @close="closeDialog"
    >

      <el-alert
         show-icon
        title="固定金额收费填写数字，比如收费5U，填写5；比例收费时填写百分比，比如收费交易金额的10%，填写10%。月租服务费仅支持固定金额"
        type="error">
      </el-alert> 
      <el-form
        :model="addForm"
        :rules="rules"
        label-width="200px"
        ref="addForm"
        class="myForm"
      >
  
         <el-form-item label="操作人"  > 
          {{ addForm.createBy || '-' }}
        </el-form-item>
        <el-form-item label="更新时间"  > 
          {{    timestampToTime(addForm.updateAt, 8 * 60 * 60)  || '-' }}
        </el-form-item>  
        <el-form-item label="开卡手续费" prop="open"  style="margin-top: 20px;">
          <el-input
            size="small"
            v-model="addForm.open"
            auto-complete="off"
            placeholder="请输入开卡手续费"
          ></el-input>
        </el-form-item>
        <el-form-item label="卡充值手续费" prop="recharge" >
          <el-input
            size="small"
            v-model="addForm.recharge"
            auto-complete="off"
            placeholder="请输入卡充值手续费"
          ></el-input>
        </el-form-item>
        <el-form-item label="交易退款手续费" prop="refund" >
          <el-input
            size="small"
            v-model="addForm.refund"
            auto-complete="off"
            placeholder="请输入交易退款手续费"
          ></el-input>
        </el-form-item>
        <el-form-item label="小额交易费(常规卡) " prop="auth" >
          <el-input
            size="small"
            v-model="addForm.auth"
            auto-complete="off"
            placeholder="请输入交易手续费(常规卡)"
          ></el-input>
        </el-form-item>
        <el-form-item label="小额交易费(共享卡) " prop="shareAuth" >
          <el-input
            size="small"
            v-model="addForm.shareAuth"
            auto-complete="off"
            placeholder="请输入小额交易费(共享卡)"
          ></el-input>
        </el-form-item>  

        <el-form-item label="共享卡交易费" prop="transactionFee" >
          <el-input
            size="small"
            v-model="addForm.transactionFee"
            auto-complete="off"
            placeholder="请输入共享卡交易费"
          ></el-input>
        </el-form-item>
        <el-form-item label="月租服务费" prop="coverCharge" >
          <el-input
            size="small"
            v-model="addForm.coverCharge"
            auto-complete="off"
            placeholder="请输入月租服务费"
          ></el-input>
        </el-form-item>

        <el-form-item label="跨币种手续费" prop="crossCurrencyFee" >
          <el-input
            size="small"
            v-model="addForm.crossCurrencyFee"
            auto-complete="off"
            placeholder="请输入跨币种手续费"
          ></el-input>
        </el-form-item> 
       
        <el-form-item label="备注" prop="remark" >
          <el-input
            size="small"
            v-model.trim="addForm.remark"
            auto-complete="off"
            type="textarea"
            :rows="5"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item> 

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('addForm')"
          >提交</el-button
        >
      </div>
    </el-dialog>
    <!-- 编辑界面 -->
    <codeChoose ref="codeRef" :fromType="verType" @setParam="getParam" ></codeChoose>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import codeChoose from '@/components/codeChoose.vue'
import {
  setStoreS,
  getStoreS,
  removeStoreS,
  insertCharacter,
  timestampToTime,
  spStr,
} from "../../utils/util";
import {
  baseURL,
  req,
  reqJson,
  encrypt,
  decrypt,
  reqtoken,
  service,
  format,
  getUrl,
} from "../../api/axiosFun";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      loading: false,
      editFormVisible: false,
      addFormVisible: false,
      addUserVisible: false,
      b: format,
      timestampToTime: timestampToTime,
      formInline: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        userId: "",
      },
      addForm: {
        auth: "",
        coverCharge: "",
        shareAuth: 0,
        name: "",
        open: "",
        recharge: "",
        refund: "",
        remark: "",
        users: [],
        crossCurrencyFee:""
      },
 
      // rules表单验证
      rules: {
        auth: [
          { required: true, message: "请输入小额交易费(常规卡)", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?\%?$/,
            message: "请输入数字(两位小数)或者百分数",
            trigger: "blur",
          },
        ],
        transactionFee: [
          { required: true, message: "请输入共享卡交易费", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?\%?$/,
            message: "请输入数字(两位小数)或者百分数",
            trigger: "blur",
          },
        ], 
        coverCharge: [
          { required: true, message: "请输入月租服务费", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.\d{1,2})?$/,
            message: "请输入正确的数字",
            trigger: "blur",
          },
        ],
        crossCurrencyFee: [
          { required: true, message: "请输入月租服务费", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.\d{1,2})?$/,
            message: "请输入正确的数字",
            trigger: "blur",
          },
        ], 
        shareAuth: [
          { required: true, message: "请输入小额交易费(共享卡)", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?\%?$/,
            message: "请输入数字(或两位小数)或者百分数",
            trigger: "blur",
          },
        ],
        name: [{ required: true, message: "请输入组名", trigger: "blur" }],
        open: [
          { required: true, message: "请输入开卡手续费", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?\%?$/,
            message: "请输入数字(两位小数)或者百分数",
            trigger: "blur",
          },
        ],
        recharge: [
          { required: true, message: "请输入卡充值手续费", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?\%?$/,
            message: "请输入数字(两位小数)或者百分数",
            trigger: "blur",
          },
        ],
        refund: [
          { required: true, message: "请输入交易退款手续费", trigger: "blur" },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?\%?$/,
            message: "请输入数字(两位小数)或者百分数",
            trigger: "blur",
          },
        ],
      },
      feeItem: "",
      userIDList: [],
      delIDList: [],
      options: [],
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },

      verArry:[],
      verType:'',
      curItem:'', 
      userInfo: null,
      fd:''

    };
  },
  // 注册组件
  components: {
    Pagination,
    codeChoose
  },
  created() {
    this.getdata(this.formInline);
    this.getInfo(); 
  },
  methods: {
    getInfo() {
      service
        .get("/api/photon/getUserInfo", {})
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.userInfo = res.data;
            this.$store.commit("user", JSON.stringify(res.data));
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    }, 
    getdata(parameter) {
      let url = getUrl(parameter);
      service
        .get("/api/admin/getUserList" + url+'&agent=1', { 
        })
        .then((res) => {
          this.loading = false;
          if (res.code == 0) { 
            this.listData = res.rows;
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.total;
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error(spStr(err, "PhotonPay", ""));
        });
    },
    getRowClass({ row }) {
      if (row.users.length == 0) {
        return "row-expand-cover";
      } else {
        return "";
      }
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNum = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },


    getParam(val,type){  
      this.headers = val ;  
      this.editSaveAuth() 
      // if(type =='add'){
      //   this.editSaveAuth() 
      // } 
      
    },  
    editSaveAuth(){  
        let obj = {
          "auth":  this.addForm.auth,
          "coverCharge":  this.addForm.coverCharge,
          // "createAt": this.editForm?.auth,
          // "createBy": this.editForm?.auth,
          "id":  this.addForm.id,
          "open":  this.addForm.open,
          "recharge":  this.addForm.recharge,
          "refund":  this.addForm.refund,
          "remark":  this.addForm.remark,
          "shareAuth":  this.addForm.shareAuth,
          // "updateAt": " this.addForm?.auth,
          "userId": this.addForm.userId,
        }

          reqJson('post',"/api/admin/editRateConfigAgent", obj,{
            headers:{
              ...this.headers
            }
          })  
          // service
          //   .post("/api/admin/editRateConfigAgent", obj)
            .then((res) => {
              this.loading = false;
              if (res.code == 0) {
                this.$message.success(res.msg);
                this.getdata(this.formInline);
                this.closeDialog();
                // this.addForm = res.data; 
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.$message.error(spStr(err, "PhotonPay", ""));
          });  
    },
    handleEdit(name, row) { 
      this.addForm = row;  
      service
        .get("/api/admin/getRateConfigAgent/"+row.userId, {
          "userId": this.addForm.userId,
        })
        .then((res) => {
          this.loading = false;
          if (res.code == 0) { 
            this.addForm =   {
              ...this.addForm,
              ...res.data
            }; 
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch((err) => {
          this.$message.error(spStr(err, "PhotonPay", ""));
      }); 
      this.addFormVisible = true;
    }, 
    submitForm(name) {
      this.$refs[name].validate((valid) => {
        if (!valid) return;
        this.loading = true;    
        if(this.userInfo&&this.userInfo.verifyApi){
            this.verArry =  this.userInfo.verifyApi.split(',');
          if(this.verArry.includes('/api/admin/editRateConfigAgent')){
            this.$refs.codeRef.openDialog(); 
            this.verType = 'add'
          }else{ 
            this.editSaveAuth();
          }
        }else{
          this.editSaveAuth();
        }   
      }) 
    }, 
    // 关闭编辑、增加弹出框
    closeDialog(name) { 
      this.addFormVisible = false;
        this.$refs.addForm.resetFields();
    },
 
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
/deep/ .el-table .row-expand-cover .cell .el-table__expand-icon {
  display: none;
}
.myForm .el-form-item {
  /* display: inline-block;
  margin-left: 5%; */
}

.el-form .select10 .el-select {
  width: 100%;
}
</style>

 
 